<?php
namespace api\modules\v1\controllers;

use Yii;
use common\models\LoginForm;
use yii\data\ActiveDataProvider;
use yii\filters\auth\QueryParamAuth;
use yii\helpers\ArrayHelper;
use yii\rest\ActiveController;


/**
 *   {
        "error_code": 0,
        "data": {
        "uid": "1",
        "username": "12154545",
        "name": "吴系挂",
        "groupid": 2 ,
        "reg_time": "1436864169",
        "last_login_time": "0",
        }
    }
 *
 * Class WxuserController
 * @package frontend\controllers
 */
class WxuserController extends ActiveController
{
    public $modelClass = 'api\modules\v1\models\Wxuser';

    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items'
    ];

    public function behaviors()
    {
        return ArrayHelper::merge (parent::behaviors(), [
            'authenticator' => [
                //access-token
                'class' => QueryParamAuth::className()
            ]
        ] );
    }

    public function actions()
    {
        $actions = parent::actions();
        // 注销系统自带的实现方法
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    /**
     * 获取用户列表
     *
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find();
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }

    /**
     * 这是一个测试的Api
     * @desc 列举所有的注释格式
     * @param string $user_type |用户类型|yes|其他说明|
     * @param int $sex |性别|no|0:不限 1:男 2:女|
     * @return int status 操作码，0表示成功
     * @return array list 用户列表
     * @return int list[].id 用户ID
     * @return string list[].name 用户名字
     * @return string msg 提示信息
     * @exception 400 参数传递错误
     * @exception 500 服务器内部错误
     */
    public function actionDemoapi($user_type, $sex)
    {
        $result = [
            'status' => 0,
            'list' => [
                'id' => 1,
                'name' => 'kaopur'
            ],
            'msg' => 'OK'
        ];
        return \yii\helpers\Json::encode($result);
    }

    /**
     * Logs in a user.
     * 登录实现
     *
     * @return mixed
     */
    public function actionLogin()
    {
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
             return  true;
        } else {
            return false;
        }
    }
}
